package com.dz.xjd.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.dz.xjd.R;
import com.dz.xjd.ui.base.BaseActivity;
import com.dz.xjd.ui.base.BaseFragment;
import com.dz.xjd.ui.fragment.LoanFragment;
import com.dz.xjd.ui.fragment.IdentifyFragment;
import com.dz.xjd.ui.fragment.PersonalFragment;
import java.util.List;
import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.layout_top_tv_left)
    TextView mTvTitleBack;

    @BindView(R.id.layout_top_tv_title)
    TextView mTvtitle;


    private LinearLayout mBottomContainer ;
    private BaseFragment[] fgs;
    private Fragment mCurrrentFragent;
    private String[] titles = {"现金贷","认证","我"};

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        initTitle();
        initView();
        initFragment();
        initListener();
    }

    @Override
    public void initPresenter() {

    }

    private void initView(){

        mBottomContainer = (LinearLayout) findViewById(R.id.ll_main_bottom);
    }

    private void initFragment(){
        fgs = new BaseFragment[]{new LoanFragment(), new IdentifyFragment(), new PersonalFragment()};
        getSupportFragmentManager().beginTransaction().add(R.id.ll_main_content, fgs[0]).commit();
        getSupportFragmentManager().beginTransaction().show(fgs[0]).commit();
        mCurrrentFragent = fgs[0];
    }

    private void initListener(){
        int count = mBottomContainer.getChildCount();
        for (int i = 0; i< count; i++){
            View button = mBottomContainer.getChildAt(i);
            button.setTag(i);
            button.setOnClickListener(onClickListener);
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            int current = (Integer)v.getTag();
            changeTitle(current);
            FragmentManager fragmentManager = getSupportFragmentManager();
            List<Fragment> list = fragmentManager.getFragments();
            BaseFragment targetFragment = fgs[current];
            if(list == null || !list.contains(targetFragment)){
                fragmentManager.beginTransaction().add(R.id.ll_main_content, targetFragment).commit();
            }
            if(list != null){
                for (Fragment fragment : list){
                    if(fragment == targetFragment) continue;
                    fragmentManager.beginTransaction().hide(fragment).commit();
                }
                fragmentManager.beginTransaction().show(targetFragment).commit();
                mCurrrentFragent=targetFragment;
            }
        }
    };

    private void initTitle(){
        mTvTitleBack.setVisibility(View.GONE);
        mTvtitle.setText(titles[0]);
    }

    private void changeTitle(int current){
        mTvtitle.setText(titles[current]);
    }
}
